<template>
  <div id="userManagePage">
    <!-- 搜索表单 -->
    <a-form :model="searchParams" layout="inline" @finish="doSearch">
      <a-form-item label="账号">
        <a-input v-model:value="searchParams.userAccount" placeholder="输入账号" />
      </a-form-item>
      <a-form-item label="用户名">
        <a-input v-model:value="searchParams.userName" placeholder="输入用户名" />
      </a-form-item>
      <a-form-item>
        <a-button html-type="submit" type="primary">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-divider />
    <!-- 表格 -->
    <a-table
      :columns="columns"
      :data-source="data"
      :pagination="pagination"
      @change="doTableChange"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'userAvatar'">
          <a-image :src="record.userAvatar" :width="120" />
        </template>
        <template v-else-if="column.dataIndex === 'userRole'">
          <div v-if="record.userRole === 'admin'">
            <a-tag color="green">管理员</a-tag>
          </div>
          <div v-else>
            <a-tag color="blue">普通用户</a-tag>
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.key === 'action'">
          <a-space>
            <a-button danger @click="doDelete(record.id)">删除</a-button>
            <a-button type="primary" @click="doUpdate(record.id)">编辑用户</a-button>
          </a-space>
        </template>
      </template>
    </a-table>

    <!-- 用户编辑模态框 -->
    <AdminUserEditModal
      v-model:open="showEditModal"
      :user-id="editUserId"
      @success="handleEditSuccess"
    />
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { deleteUser, listUserVoByPage } from '@/api/userController.ts'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import AdminUserEditModal from '@/components/AdminUserEditModal.vue'


const columns = [
  {
    title: 'id',
    dataIndex: 'id',
  },
  {
    title: '账号',
    dataIndex: 'userAccount',
  },
  {
    title: '用户名',
    dataIndex: 'userName',
  },
  {
    title: '头像',
    dataIndex: 'userAvatar',
  },
  {
    title: '简介',
    dataIndex: 'userProfile',
  },
  {
    title: '用户角色',
    dataIndex: 'userRole',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]

// 展示的数据
const data = ref<API.UserVO[]>([])
const total = ref(0)

// 编辑模态框相关
const showEditModal = ref(false)
const editUserId = ref<number>()

// 搜索条件
const searchParams = reactive<API.UserQueryRequest>({
  pageNum: 1,
  pageSize: 10,
})

// 获取数据
const fetchData = async () => {
  const res = await listUserVoByPage({
    ...searchParams,
  })
  if (res.data.data) {
    data.value = res.data.data.records ?? []
    total.value = res.data.data.totalRow ?? 0
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
}

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.pageNum ?? 1,
    pageSize: searchParams.pageSize ?? 10,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total: number) => `共 ${total} 条`,
  }
})

// 表格分页变化时的操作
const doTableChange = (page: { current: number; pageSize: number }) => {
  searchParams.pageNum = page.current
  searchParams.pageSize = page.pageSize
  fetchData()
}

// 搜索数据
const doSearch = () => {
  // 重置页码
  searchParams.pageNum = 1
  fetchData()
}

// 删除数据
const doDelete = async (id: string) => {
  if (!id) {
    return
  }
  const res = await deleteUser({ id: Number(id) })
  if (res.data.code === 0) {
    message.success('删除成功')
    // 刷新数据
    await fetchData()
  } else {
    message.error('删除失败')
  }
}

const doUpdate = async (id: number) => {
  if (!id) {
    return
  }
  editUserId.value = id
  showEditModal.value = true
}

// 处理编辑成功
const handleEditSuccess = () => {
  message.success('用户信息更新成功')
  fetchData() // 刷新数据
}

// 页面加载时请求一次
onMounted(() => {
  fetchData()
})
</script>

<style scoped>
#userManagePage {
  padding: 32px;
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 25%, #334155 50%, #475569 100%);
  min-height: calc(100vh - 64px);
  position: relative;
  overflow: hidden;
}

/* 科技感背景网格 */
#userManagePage::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    linear-gradient(rgba(0, 255, 255, 0.05) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0, 255, 255, 0.05) 1px, transparent 1px),
    linear-gradient(rgba(138, 43, 226, 0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(138, 43, 226, 0.04) 1px, transparent 1px);
  background-size:
    100px 100px,
    100px 100px,
    20px 20px,
    20px 20px;
  pointer-events: none;
  animation: gridFloat 20s ease-in-out infinite;
}

@keyframes gridFloat {
  0%, 100% { transform: translate(0, 0); }
  50% { transform: translate(2px, 2px); }
}

/* 动态光效 */
#userManagePage::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
    radial-gradient(circle at 20% 30%, rgba(0, 255, 255, 0.08) 0%, transparent 50%),
    radial-gradient(circle at 80% 70%, rgba(138, 43, 226, 0.06) 0%, transparent 50%),
    radial-gradient(circle at 50% 50%, rgba(0, 255, 127, 0.04) 0%, transparent 50%);
  pointer-events: none;
  animation: lightPulse 8s ease-in-out infinite alternate;
}

@keyframes lightPulse {
  0% { opacity: 0.5; }
  100% { opacity: 1; }
}

/* 内容区域 */
#userManagePage > * {
  position: relative;
  z-index: 2;
}

/* 搜索表单样式 */
:deep(.ant-form) {
  background: rgba(15, 23, 42, 0.8);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(0, 255, 255, 0.2);
  border-radius: 16px;
  padding: 24px;
  margin-bottom: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

:deep(.ant-form-item-label > label) {
  color: #e2e8f0 !important;
  font-weight: 500;
}

:deep(.ant-input) {
  background: rgba(15, 23, 42, 0.8) !important;
  border: 1px solid rgba(0, 255, 255, 0.3) !important;
  color: #e2e8f0 !important;
  border-radius: 8px;
}

:deep(.ant-input:focus) {
  border-color: rgba(0, 255, 255, 0.6) !important;
  box-shadow: 0 0 0 2px rgba(0, 255, 255, 0.2) !important;
}

:deep(.ant-input::placeholder) {
  color: #64748b !important;
}

:deep(.ant-btn-primary) {
  background: linear-gradient(135deg, #00ffff, #8a2be2) !important;
  border: none !important;
  border-radius: 8px;
  font-weight: 600;
  box-shadow: 0 4px 15px rgba(0, 255, 255, 0.3);
  transition: all 0.3s ease;
}

:deep(.ant-btn-primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 255, 255, 0.5);
}

/* 表格样式 */
:deep(.ant-table) {
  background: rgba(15, 23, 42, 0.8) !important;
  backdrop-filter: blur(20px);
  border: 1px solid rgba(0, 255, 255, 0.2);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

:deep(.ant-table-thead > tr > th) {
  background: rgba(30, 41, 59, 0.9) !important;
  color: #e2e8f0 !important;
  border-bottom: 1px solid rgba(0, 255, 255, 0.2) !important;
  font-weight: 600;
  font-size: 14px;
}

:deep(.ant-table-tbody > tr > td) {
  background: rgba(15, 23, 42, 0.6) !important;
  color: #cbd5e1 !important;
  border-bottom: 1px solid rgba(0, 255, 255, 0.1) !important;
}

:deep(.ant-table-tbody > tr:hover > td) {
  background: rgba(0, 255, 255, 0.1) !important;
  color: #e2e8f0 !important;
}

/* 按钮样式 */
:deep(.ant-btn-default) {
  background: rgba(15, 23, 42, 0.8) !important;
  border: 1px solid rgba(0, 255, 255, 0.3) !important;
  color: #cbd5e1 !important;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
}

:deep(.ant-btn-default:hover) {
  border-color: rgba(0, 255, 255, 0.6) !important;
  color: #00ffff !important;
  background: rgba(0, 255, 255, 0.1) !important;
  transform: translateY(-1px);
}

:deep(.ant-btn-dangerous) {
  background: linear-gradient(135deg, #ef4444, #dc2626) !important;
  border: none !important;
  color: white !important;
  border-radius: 8px;
  font-weight: 500;
}

:deep(.ant-btn-dangerous:hover) {
  transform: translateY(-1px);
  box-shadow: 0 4px 15px rgba(239, 68, 68, 0.4);
}

/* 标签样式 */
:deep(.ant-tag) {
  border-radius: 6px;
  font-weight: 500;
  border: none;
}

:deep(.ant-tag-green) {
  background: rgba(0, 255, 127, 0.2) !important;
  color: #00ff7f !important;
  border: 1px solid rgba(0, 255, 127, 0.4) !important;
}

:deep(.ant-tag-blue) {
  background: rgba(0, 255, 255, 0.2) !important;
  color: #00ffff !important;
  border: 1px solid rgba(0, 255, 255, 0.4) !important;
}

/* 分页样式 */
:deep(.ant-pagination) {
  margin-top: 24px;
  text-align: center;
}

:deep(.ant-pagination-item) {
  background: rgba(15, 23, 42, 0.8) !important;
  border: 1px solid rgba(0, 255, 255, 0.3) !important;
  border-radius: 6px;
}

:deep(.ant-pagination-item a) {
  color: #cbd5e1 !important;
}

:deep(.ant-pagination-item-active) {
  background: linear-gradient(135deg, #00ffff, #8a2be2) !important;
  border-color: transparent !important;
}

:deep(.ant-pagination-item-active a) {
  color: white !important;
}

:deep(.ant-pagination-prev, .ant-pagination-next) {
  background: rgba(15, 23, 42, 0.8) !important;
  border: 1px solid rgba(0, 255, 255, 0.3) !important;
  border-radius: 6px;
}

:deep(.ant-pagination-prev a, .ant-pagination-next a) {
  color: #cbd5e1 !important;
}

/* 分割线样式 */
:deep(.ant-divider) {
  border-color: rgba(0, 255, 255, 0.2) !important;
}

/* 图片样式 */
:deep(.ant-image) {
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid rgba(0, 255, 255, 0.2);
}
</style>
